package com.cxf.windex.task;

import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.biz.BConfigBiz;
import com.cxf.windex.biz.BOrdersBiz;
import com.cxf.windex.biz.BOrdersTaskBiz;
import com.cxf.windex.biz.JsUserBiz;
import com.cxf.windex.biz.MoneyBiz;
import com.cxf.windex.entity.BConfig;
import com.cxf.windex.entity.BOrders;
import com.cxf.windex.entity.BOrdersTask;
import com.cxf.windex.entity.JsUser;
import com.cxf.windex.enums.OperateEnum;
import java.math.BigDecimal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @program: business_needs
 * @description: 奖励任务
 * @author: chenxf
 * @create: 2018-11-08 17:13
 **/
@Component
public class BonusTask {

  @Autowired
  private BOrdersBiz ordersBiz;
  @Autowired
  private BOrdersTaskBiz ordersTaskBiz;
  @Autowired
  private MoneyBiz moneyBiz;
  @Autowired
  private JsUserBiz jsUserBiz;
  @Autowired
  private BConfigBiz configBiz;
  @Autowired
  @Lazy
  private LogTask logTask;
  /**
   * 发放奖励
   * @param ordersTaskId
   */
  @Async
  public void doBonus(Long ordersTaskId){
    BOrdersTask ordersTask=ordersTaskBiz.selectById(ordersTaskId);
    BConfig config;
    if(StringUtils.isBlank(ordersTask.getShopWxopenid())){
       config=configBiz.getInfo(UserConstant.CONFIG_ADMIN);
    }else{
       config=configBiz.getInfo(UserConstant.CONFIG_LW);
    }

    //订单已支付金额增加
    BOrders orders=ordersBiz.selectById(ordersTask.getOrderId());
    ordersBiz.updateSelectiveById(BOrders.builder().id(orders.getId()).payPrice(orders.getPayPrice().add(ordersTask.getPayPrice())).build());

    //支付福蛙
    logTask.payMoney(ordersTask.getResourcesWxopenid(),ordersTask.getRealPrice(), OperateEnum.INCOME_BONUS,UserConstant.RESOURCE_RANGE,ordersTask.getId().toString());

    BigDecimal bonusMoney=ordersTask.getPayPrice().divide(BigDecimal.valueOf(100));
    //支付福蛙上线
    JsUser jsUser=jsUserBiz.queryByOpenid(ordersTask.getResourcesWxopenid());
    if(StringUtils.isNotBlank(jsUser.getCrtUser())){
      jsUser=jsUserBiz.selectById(jsUser.getCrtUser());
      BigDecimal fpBonus=BigDecimal.ZERO;
      if("0".equals(ordersTask.getType())){
        fpBonus= bonusMoney.multiply(config.getLFP());
      }else {
        fpBonus=bonusMoney.multiply(config.getVFP());
      }
      logTask.payMoney(jsUser.getOpenid(),fpBonus, OperateEnum.INCOME_COMMISSION,UserConstant.RESOURCE_RANGE,ordersTask.getId().toString());
    }

    //支付路蛙上线
    jsUser=jsUserBiz.queryByOpenid(ordersTask.getShopWxopenid());
    if(StringUtils.isNotBlank(jsUser.getCrtUser())){
      jsUser=jsUserBiz.selectById(jsUser.getCrtUser());
      BigDecimal lpBonus=BigDecimal.ZERO;
      if("0".equals(ordersTask.getType())){
        lpBonus= bonusMoney.multiply(config.getLLP());
      }else {
        lpBonus=bonusMoney.multiply(config.getVLP());
      }
      logTask.payMoney(jsUser.getOpenid(),lpBonus, OperateEnum.INCOME_COMMISSION,UserConstant.SHOP_RANGE,ordersTask.getId().toString());
    }

  }


}
